﻿namespace Yipusoft.BaseDoc
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Reflection;

    public class Doc
    {
        public static bool DeleteBaseDoc(string ID)
        {
            ParameterExpression expression;
            BaseDocDataContext context = new BaseDocDataContext();
            BaseDocs entity = context.BaseDocs.Where<BaseDocs>(Expression.Lambda<Func<BaseDocs, bool>>(Expression.Equal(Expression.Property(expression = Expression.Parameter(typeof(BaseDocs), "o"), (MethodInfo) methodof(BaseDocs.get_ID)), Expression.New((ConstructorInfo) methodof(Guid..ctor), new Expression[] { Expression.Constant(ID) }), false, (MethodInfo) methodof(Guid.op_Equality)), new ParameterExpression[] { expression })).FirstOrDefault<BaseDocs>();
            context.BaseDocs.DeleteOnSubmit(entity);
            context.SubmitChanges();
            return true;
        }

        public static bool DeleteBaseDocFileByDID(string dID)
        {
            ParameterExpression expression;
            BaseDocDataContext context = new BaseDocDataContext();
            BaseDocFiles entity = context.BaseDocFiles.Where<BaseDocFiles>(Expression.Lambda<Func<BaseDocFiles, bool>>(Expression.Equal(Expression.Property(expression = Expression.Parameter(typeof(BaseDocFiles), "obj"), (MethodInfo) methodof(BaseDocFiles.get_DID)), Expression.Convert(Expression.New((ConstructorInfo) methodof(Guid..ctor), new Expression[] { Expression.Constant(dID) }), typeof(Guid?)), false, (MethodInfo) methodof(Guid.op_Equality)), new ParameterExpression[] { expression })).FirstOrDefault<BaseDocFiles>();
            if (entity != null)
            {
                context.BaseDocFiles.DeleteOnSubmit(entity);
                context.SubmitChanges();
            }
            return true;
        }

        public static bool DeleteBaseDocFileByID(string ID)
        {
            ParameterExpression expression;
            BaseDocDataContext context = new BaseDocDataContext();
            BaseDocFiles entity = context.BaseDocFiles.Where<BaseDocFiles>(Expression.Lambda<Func<BaseDocFiles, bool>>(Expression.Equal(Expression.Property(expression = Expression.Parameter(typeof(BaseDocFiles), "obj"), (MethodInfo) methodof(BaseDocFiles.get_ID)), Expression.New((ConstructorInfo) methodof(Guid..ctor), new Expression[] { Expression.Constant(ID) }), false, (MethodInfo) methodof(Guid.op_Equality)), new ParameterExpression[] { expression })).FirstOrDefault<BaseDocFiles>();
            context.BaseDocFiles.DeleteOnSubmit(entity);
            context.SubmitChanges();
            return true;
        }

        public static BaseDocs GetBaseDoc(string ID)
        {
            ParameterExpression expression;
            BaseDocDataContext context = new BaseDocDataContext();
            return context.BaseDocs.Where<BaseDocs>(Expression.Lambda<Func<BaseDocs, bool>>(Expression.Equal(Expression.Property(expression = Expression.Parameter(typeof(BaseDocs), "obj"), (MethodInfo) methodof(BaseDocs.get_ID)), Expression.New((ConstructorInfo) methodof(Guid..ctor), new Expression[] { Expression.Constant(ID) }), false, (MethodInfo) methodof(Guid.op_Equality)), new ParameterExpression[] { expression })).FirstOrDefault<BaseDocs>();
        }

        public static IEnumerable<BaseDocFiles> GetBaseDocFileByDID(string dID)
        {
            ParameterExpression expression;
            BaseDocDataContext context = new BaseDocDataContext();
            return context.BaseDocFiles.Where<BaseDocFiles>(Expression.Lambda<Func<BaseDocFiles, bool>>(Expression.Equal(Expression.Property(expression = Expression.Parameter(typeof(BaseDocFiles), "obj"), (MethodInfo) methodof(BaseDocFiles.get_DID)), Expression.Convert(Expression.New((ConstructorInfo) methodof(Guid..ctor), new Expression[] { Expression.Constant(dID) }), typeof(Guid?)), false, (MethodInfo) methodof(Guid.op_Equality)), new ParameterExpression[] { expression }));
        }

        public static IEnumerable<BaseDocFiles> GetBaseDocFileList()
        {
            ParameterExpression expression;
            BaseDocDataContext context = new BaseDocDataContext();
            return context.BaseDocFiles.Select<BaseDocFiles, BaseDocFiles>(Expression.Lambda<Func<BaseDocFiles, BaseDocFiles>>(expression = Expression.Parameter(typeof(BaseDocFiles), "obj"), new ParameterExpression[] { expression }));
        }

        public static IQueryable GetBaseDocList()
        {
            ParameterExpression expression;
            BaseDocDataContext context = new BaseDocDataContext();
            return context.BaseDocs.Select<BaseDocs, BaseDocs>(Expression.Lambda<Func<BaseDocs, BaseDocs>>(expression = Expression.Parameter(typeof(BaseDocs), "obj"), new ParameterExpression[] { expression }));
        }

        public static IEnumerable<BaseDocs> GetBaseDocList2()
        {
            ParameterExpression expression;
            BaseDocDataContext context = new BaseDocDataContext();
            return context.BaseDocs.Select<BaseDocs, BaseDocs>(Expression.Lambda<Func<BaseDocs, BaseDocs>>(expression = Expression.Parameter(typeof(BaseDocs), "obj"), new ParameterExpression[] { expression }));
        }

        public static BaseDocType GetDocType(int ID)
        {
            ParameterExpression expression;
            BaseDocDataContext context = new BaseDocDataContext();
            return context.BaseDocType.Where<BaseDocType>(Expression.Lambda<Func<BaseDocType, bool>>(Expression.Equal(Expression.Property(expression = Expression.Parameter(typeof(BaseDocType), "obj"), (MethodInfo) methodof(BaseDocType.get_ID)), Expression.Constant(ID)), new ParameterExpression[] { expression })).FirstOrDefault<BaseDocType>();
        }

        public static IEnumerable<BaseDocType> GetDocTypeList()
        {
            ParameterExpression expression;
            BaseDocDataContext context = new BaseDocDataContext();
            return context.BaseDocType.Select<BaseDocType, BaseDocType>(Expression.Lambda<Func<BaseDocType, BaseDocType>>(expression = Expression.Parameter(typeof(BaseDocType), "obj"), new ParameterExpression[] { expression }));
        }

        public static string InsertDoc(string caption, string contents, string setUserID, string departmentID, DateTime createDate, string remark, int tID)
        {
            BaseDocDataContext context = new BaseDocDataContext();
            Guid guid = Guid.NewGuid();
            BaseDocs entity = new BaseDocs();
            entity.ID = guid;
            entity.Caption = caption;
            entity.Contents = contents;
            entity.Remark = remark;
            entity.CreateDate = new DateTime?(createDate);
            entity.SetDepartment = departmentID;
            entity.SetUserID = setUserID;
            entity.TID = new int?(tID);
            context.BaseDocs.InsertOnSubmit(entity);
            context.SubmitChanges();
            return guid.ToString();
        }

        public static bool InsertDocFile(string dID, string filetype, string fileName, string fileTitle)
        {
            BaseDocDataContext context = new BaseDocDataContext();
            BaseDocFiles entity = new BaseDocFiles();
            entity.ID = Guid.NewGuid();
            entity.DID = new Guid(dID);
            entity.FileType = filetype;
            entity.FileName = fileName;
            entity.FileTitle = fileTitle;
            context.BaseDocFiles.InsertOnSubmit(entity);
            context.SubmitChanges();
            return true;
        }

        public static string UpdateDoc(BaseDocs doc)
        {
            ParameterExpression expression;
            BaseDocDataContext context = new BaseDocDataContext();
            BaseDocs docs = context.BaseDocs.Where<BaseDocs>(Expression.Lambda<Func<BaseDocs, bool>>(Expression.Equal(Expression.Property(expression = Expression.Parameter(typeof(BaseDocs), "obj"), (MethodInfo) methodof(BaseDocs.get_ID)), Expression.Property(Expression.Constant(doc), (MethodInfo) methodof(BaseDocs.get_ID)), false, (MethodInfo) methodof(Guid.op_Equality)), new ParameterExpression[] { expression })).FirstOrDefault<BaseDocs>();
            docs.CreateDate = doc.CreateDate;
            docs.Contents = doc.Contents;
            docs.Caption = doc.Caption;
            docs.Remark = doc.Remark;
            docs.SetDepartment = doc.SetDepartment;
            docs.SetUserID = doc.SetUserID;
            docs.TID = doc.TID;
            context.SubmitChanges();
            return doc.ID.ToString();
        }
    }
}

